package com.chenjun.common.util.alarm;

import java.util.Queue;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

public class ZeepUdpCommShortHandler implements IoHandler{
    
	public Queue<byte[]> queue;
	public ZeepUdpCommShortHandler()
	{
		
	}
	public ZeepUdpCommShortHandler(Queue<byte[]> queue) {
		super();
		this.queue = queue;
	}
	@Override
	public void exceptionCaught(IoSession session, Throwable ex)
			throws Exception {
		// TODO Auto-generated method stub
		ex.printStackTrace();
		session.close(true);
		
	}

	@Override
	public void messageReceived(IoSession session, Object message) throws Exception {
		// TODO Auto-generated method stub
		IoBuffer io = (IoBuffer) message;
		if (io.hasRemaining())
		{
			byte[] validBytes = new byte[io.remaining()];
			io.get(validBytes,0,io.remaining());
			if ((validBytes.length == ZeepUdpCommShort.WGPacketSize)
					&& (validBytes[0] == ZeepUdpCommShort.Type))  //型号固定
			{
				synchronized (queue)
		         {
  				   queue.offer(validBytes);
		         }
			}
			else
			{
			
			}
			
		}
		
	}

	@Override
	public void messageSent(IoSession session, Object message) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void sessionClosed(IoSession arg0) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void sessionCreated(IoSession arg0) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void sessionIdle(IoSession arg0, IdleStatus arg1) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void sessionOpened(IoSession arg0) throws Exception {
		// TODO Auto-generated method stub
		
	}

}
